package com.lc202406;

public class Solution14 {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            prefix = samePrefix(prefix, strs[i]);
            if (prefix.equals("")) {
                return "";
            }
        }
        return prefix;
    }

    public static String samePrefix(String str1, String str2) {
        int index = 0;
        for (int i = 0; i < str1.length() && i < str2.length(); i++) {
            if (str1.charAt(i) != str2.charAt(i)) {
                break;
            }
            index++;
        }
        return str1.substring(0, index);
    }

    public static void main(String[] args) {

    }
}
